/* Medical Calculator
Copyright (c) 2009, John Cronin
All rights reserved.

Redistribution and use in source and binary forms, with or without modification, are 
permitted provided that the following conditions are met:

- Redistributions of source code must retain the above copyright notice, this list 
  of conditions and the following disclaimer.

- Redistributions in binary form must reproduce the above copyright notice, this list
  of conditions and the following disclaimer in the documentation and/or other materials 
  provided with the distribution.

- Neither the name of the the Author nor the names of its contributors may be used to 
  endorse or promote products derived from this software without specific prior written 
  permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS 
OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;

namespace MedicalCalculator
{
	/// <summary>
	/// Description of KeyPad.
	/// </summary>
	public partial class KeyPad : UserControl
	{
		public KeyPad()
		{
			//
			// The InitializeComponent() call is required for Windows Forms designer support.
			//
			InitializeComponent();
			
			//
			// TODO: Add constructor code after the InitializeComponent() call.
			//
		}
		
		public class NumberKeyPressArgs : EventArgs
		{
			public int Number;
			public NumberKeyPressArgs(int number) { Number = number; }
		}
		public delegate void NumberKeyPressHandler(object sender, NumberKeyPressArgs e);
		
		public event NumberKeyPressHandler NumberKeyPressed;
		public event EventHandler DeletePressed;
		public event EventHandler PointPressed;
		public event EventHandler OKPressed;
		public event EventHandler CancelPressed;
		public event EventHandler BackPressed;
		
		protected virtual void OnNumberKeyPressed(NumberKeyPressArgs e)
		{
			if(NumberKeyPressed != null)
				NumberKeyPressed(this, e);
		}
		protected virtual void OnDeletePressed(EventArgs e)
		{
			if(DeletePressed != null)
				DeletePressed(this, e);
		}
		protected virtual void OnPointPressed(EventArgs e)
		{
			if(PointPressed != null)
				PointPressed(this, e);
		}
		protected virtual void OnOKPressed(EventArgs e)
		{
			if(OKPressed != null)
				OKPressed(this, e);
		}
		protected virtual void OnCancelPressed(EventArgs e)
		{
			if(CancelPressed != null)
				CancelPressed(this, e);
		}
		protected virtual void OnBackPressed(EventArgs e)
		{
			if(BackPressed != null)
				BackPressed(this, e);
		}
		
		void KeyPadResize(object sender, EventArgs e)
		{
			doResize();
		}
		
		void doResize()
		{
			int cwidth = this.Width / 3;
			int cheight = this.Height / 5;
		
			button1.Size = button2.Size = button3.Size = button4.Size = button5.Size = button6.Size =
				button7.Size = button8.Size = button9.Size = button10.Size = button11.Size =
				button12.Size = button13.Size = button14.Size = button15.Size = new Size(cwidth, cheight);
			if((cwidth * 3) != this.Width)
				button3.Width = button6.Width = button9.Width = button12.Width = button15.Width =
					button3.Width + (this.Width - cwidth * 3);
			if((cheight * 5) != this.Height)
				button13.Height = button14.Height = button15.Height =
					button13.Height + (this.Height - cheight * 5);
			
			button1.Location = new Point(0, 0);
			button2.Location = new Point(cwidth, 0);
			button3.Location = new Point(cwidth * 2, 0);
			button4.Location = new Point(0, cheight);
			button5.Location = new Point(cwidth, cheight);
			button6.Location = new Point(cwidth * 2, cheight);
			button7.Location = new Point(0, cheight * 2);
			button8.Location = new Point(cwidth, cheight * 2);
			button9.Location = new Point(cwidth * 2, cheight * 2);
			button10.Location = new Point(0, cheight * 3);
			button11.Location = new Point(cwidth, cheight * 3);
			button12.Location = new Point(cwidth * 2, cheight * 3);
			button13.Location = new Point(0, cheight * 4);
			button14.Location = new Point(cwidth, cheight * 4);
			button15.Location = new Point(cwidth * 2, cheight * 4);
		}
		
		void Button1Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(7));
		}
		
		void Button2Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(8));
		}
		
		void Button3Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(9));
		}
		
		void Button4Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(4));
		}
		
		void Button5Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(5));
		}
		
		void Button6Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(6));
		}
		
		void Button7Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(1));
		}
		
		void Button8Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(2));
		}
		
		void Button9Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(3));
		}
		
		void Button10Click(object sender, EventArgs e)
		{
			OnNumberKeyPressed(new NumberKeyPressArgs(0));
		}
		
		void Button11Click(object sender, EventArgs e)
		{
			OnPointPressed(new EventArgs());
		}
		
		void Button12Click(object sender, EventArgs e)
		{
			OnDeletePressed(new EventArgs());
		}
		
		void Button13Click(object sender, EventArgs e)
		{
			OnOKPressed(new EventArgs());
		}
		
		void Button14Click(object sender, EventArgs e)
		{
			OnCancelPressed(new EventArgs());
		}
		
		void Button15Click(object sender, EventArgs e)
		{
			OnBackPressed(new EventArgs());
		}
		
		public int ActualHeight
		{
			get
			{
				if(disp_numeric)
					return this.Height;
				else
					return button13.Height;
			}
		}
		
		bool disp_numeric = true;
		public bool DisplayNumericPad
		{
			get
			{
				return disp_numeric;
			}
			set
			{
				disp_numeric = value;
				if(disp_numeric)
				{
					button1.Visible = button2.Visible = button3.Visible = button4.Visible =
						button5.Visible = button6.Visible = button7.Visible = button8.Visible =
						button9.Visible = button10.Visible = button11.Visible = button12.Visible =
						true;
				}
				else
				{
					button1.Visible = button2.Visible = button3.Visible = button4.Visible =
						button5.Visible = button6.Visible = button7.Visible = button8.Visible =
						button9.Visible = button10.Visible = button11.Visible = button12.Visible =
						false;
				}
			}
		}
	}
}
